//
//  PSShowTextCell.m
//  Btpin
//
//  Created by Vic on 2019/3/24.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "PSShowTextCell.h"

@interface PSShowTextCell ()
{
    UILabel *textLbl;
    UILabel *detailLbl;
    UILabel *rightTextLbl;
    UIImageView *iconImgView;
}
@end

@implementation PSShowTextCell

- (instancetype)initWithFrame:(CGRect)frame target:(id)target psShowTextCellType:(PSShowTextCellType)type text:(nullable NSString *)text detail:(nullable NSString *)detail showIcon:(BOOL)showIcon rightText:(nullable NSString *)rightText showBottomLine:(BOOL)showBottomLine addBtn:(BOOL)addBtn {
    self = [super initWithFrame:frame];
    if (self) {
        [self initUIWithPSShowTextCellType:type text:text detail:detail showIcon:showIcon rightText:rightText showBottomLine:showBottomLine addBtn:addBtn];
        self.delegate = target;
    }
    return self;
}
- (void)initUIWithPSShowTextCellType:(PSShowTextCellType)type text:(nullable NSString *)text detail:(nullable NSString *)detail showIcon:(BOOL)showIcon rightText:(nullable NSString *)rightText showBottomLine:(BOOL)showBottomLine addBtn:(BOOL)addBtn {
    CGRect textFrame = CGRectMake(0, 0, 100, self.frame.size.height);
    UIColor *textColor = THEMECOLOR_OF_TEXT_333333;
    UIFont *textFont = FontBold(Font_17);
    NSInteger textFontInt = Font_17;
    
    CGRect detailFrame = CGRectMake(0, 0, self.frame.size.width-110*Scale, 25*Scale);
    
    switch (type) {
        case PSShowTextCellTypeOfPrimaryTitle: {
            textFrame = CGRectMake(0, 0, self.frame.size.width-110*Scale, self.frame.size.height);
            textColor = THEMECOLOR_OF_TEXT_333333;
            textFont = FontBold(Font_17);
            textFontInt = Font_17;
            
            detailFrame = CGRectMake(0, 0, 0.00001, 0.00001);
        } break;
        case PSShowTextCellTypeOfSecondaryTitle: {
            textFrame = CGRectMake(0, 0, self.frame.size.width-110*Scale, self.frame.size.height);
            textColor = THEMECOLOR_OF_TEXT_333333;
            textFont = FontBold(Font_15);
            textFontInt = Font_15;
            
            detailFrame = CGRectMake(0, 0, 0.00001, 0.00001);
        } break;
        case PSShowTextCellTypeOfAll: {
            CGFloat width = [UIToolsControl textWidth:text size:CGSizeMake(MAXFLOAT, 25*Scale) font:Font_15]+10*Scale;
            textFrame = CGRectMake(0, 5*Scale, width, 25*Scale);
            textColor = THEMECOLOR_OF_TEXT_333333;
            textFont = FontBold(Font_15);
            textFontInt = Font_15;
            
            detailFrame = CGRectMake(0, 30*Scale, self.frame.size.width-110*Scale, 25*Scale);
        } break;
        default:
            break;
    }
    
    textLbl = [UIToolsControl initLabelWithFrame:textFrame text:text textColor:textColor font:textFontInt];
    textLbl.font = textFont;
    [self addSubview:textLbl];
    
    detailLbl = [UIToolsControl initLabelWithFrame:detailFrame text:detail textColor:THEMECOLOR_OF_TEXT_666666 font:Font_13];
    [self addSubview:detailLbl];
    
    rightTextLbl = [UIToolsControl initLabelWithFrame:CGRectMake(0, 0, self.frame.size.width-20*Scale, self.frame.size.height) text:rightText textColor:THEMECOLOR_OF_TEXT_9a9a9a font:Font_12];
    rightTextLbl.textAlignment = NSTextAlignmentRight;
    rightTextLbl.backgroundColor = COLOR_OF_CLEAR;
    [self addSubview:rightTextLbl];
    
    iconImgView = [UIToolsControl initImageViewWithFrame:CGRectMake(EndX(textLbl), 10*Scale, 17*Scale, 15*Scale) imageName:@"cert_icon"];
    iconImgView.hidden = !showIcon;
    [self addSubview:iconImgView];
    
    UIImageView *rightImgView = [UIToolsControl initImageViewWithFrame:CGRectMake(self.frame.size.width-8*Scale, (self.frame.size.height-13*Scale)*0.5, 7*Scale, 13*Scale) imageName:@"jiantou_right"];
    [self addSubview:rightImgView];
    
    if (showBottomLine) {
        [self borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:lineH borderType:BorderSideTypeBottom];
    }
    
    if (addBtn) {
        UIButton *btn = [UIToolsControl initButtonWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) imageName:nil target:self action:@selector(btnTap) title:nil titleColor:nil];
        [self addSubview:btn];
    }
}
- (void)reloadDataWithPSShowTextCellType:(PSShowTextCellType)type text:(nullable NSString *)text detail:(nullable NSString *)detail showIcon:(BOOL)showIcon rightText:(nullable NSString *)rightText showBottomLine:(BOOL)showBottomLine {
    
    CGFloat width = [UIToolsControl textWidth:text size:CGSizeMake(MAXFLOAT, 25*Scale) font:Font_15]+10*Scale;
    textLbl.frame = CGRectMake(0, 5*Scale, width, 25*Scale);
    
    textLbl.text = text;
    detailLbl.text = detail;
    rightTextLbl.text = rightText;
    
    iconImgView.frame = CGRectMake(EndX(textLbl), 10*Scale, 17*Scale, 15*Scale);
    iconImgView.hidden = !showIcon;
}
- (void)btnTap {
    if (self.delegate && [self.delegate respondsToSelector:@selector(psShowTextCellTap:)]) {
        [self.delegate psShowTextCellTap:self];
    }
}
- (void)reloadDataForRightText:(NSString *)rightText {
    rightTextLbl.text = rightText;
}

@end
